package com.yuren.codecrushlearn.Q5200;


import com.yuren.codecrushlearn.entities.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * 二叉树的前中后序遍历
 * @author 10204
 * @date 2025-05-04 07:39
 */
public class Q5271 {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        preorder(root, ret);
        return ret;
    }

    private void preorder(TreeNode t, List<Integer> ret) {
        if (t == null) {
            return;
        }
        ret.add(t.val);
        preorder(t.left, ret);
        preorder(t.right, ret);
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        inorder(root, ret);
        return ret;
    }

    private void inorder(TreeNode t, List<Integer> ret) {
        if (t == null) {
            return;
        }
        inorder(t.left, ret);
        ret.add(t.val);
        inorder(t.right, ret);
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        postorder(root, ret);
        return ret;
    }

    private void postorder(TreeNode t, List<Integer> ret) {
        if (t == null) {
            return;
        }
        postorder(t.left, ret);
        postorder(t.right, ret);
        ret.add(t.val);
    }
}
